package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("login")
    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 10)
    public JsonResult login(@Validated @RequestBody UserLoginParam userLoginParam){
        log.debug("登录功能:userLoginParam={}",userLoginParam);
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }

    @GetMapping("select")
    @Operation(summary = "查询用户")
    @ApiOperationSupport(order = 20)
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("查询用户:userQuery={}",userQuery);
        List<UserVO> list = userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("save")
    @Operation(summary = "保存用户")
    @ApiOperationSupport(order = 30)
    public JsonResult saveUser(@Validated @RequestBody UserSaveParam userSaveParam){
        log.debug("保存用户:{}",userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @Operation(summary = "密码重置")
    @PostMapping("update/password/{userId}")
    @ApiOperationSupport(order = 40)
    public JsonResult resetPassword(@PathVariable Long userId){
        log.debug("密码重置:{}",userId);
        userService.resetPassword(userId);
        return JsonResult.ok();
    }

    @Operation(summary = "修改状态")
    @PostMapping("/update/status/{userId}/{status}")
    @ApiOperationSupport(order = 50)
    public JsonResult updateStatus(
            @PathVariable Long userId,@PathVariable String status){
        log.debug("修改状态:userId={},status={}",userId,status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }

    @Operation(summary = "删除用户")
    @PostMapping("/delete/{userId}")
    @ApiOperationSupport(order = 60)
    public JsonResult deleteUser(@PathVariable Long userId){
        log.debug("删除用户:{}",userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }

}